//
//  GCD_Communication_VC.m
//  thread
//
//  Created by 关旭 on 2019/3/11.
//  Copyright © 2019 guanxu. All rights reserved.
//

#import "GCD_Communication_VC.h"

@interface GCD_Communication_VC ()

@end

@implementation GCD_Communication_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self communication];
}

//例举一个简单的线程通讯例子，异步执行完耗时操作，返回主线程,执行主线程操作
- (void)communication {
//    获取全局并发队列
    dispatch_queue_t queue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    获取主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
//        异步任务
        for (int i=0; i<2; i++) {
            [NSThread sleepForTimeInterval:2];//模拟耗时操作
            NSLog(@"1------%@",[NSThread currentThread]);
        }
        
//        返回到主线程
        dispatch_async(mainQueue, ^{
//            执行主线程任务
            NSLog(@"2-----%@",[NSThread currentThread]);
        });
    });
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
